package com.woniuxy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.IProjects;
import com.woniuxy.entity.IprojectsDto;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.IProjectsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (IProjects)表控制层
 *
 * @author makejava
 * @since 2021-05-15 14:52:36
 */
@RestController
@RequestMapping("iProjects")
public class IProjectsController {
    /**
     * 服务对象
     */
    @Resource
    private IProjectsService iProjectsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public IProjects selectOne(Integer id) {
        return this.iProjectsService.queryById(id);
    }

    /**
     * Description : Iproject表跟project表联查所有数据  <br/>
     * ChangeLog : 1. 创建 ( 20:33 );
     * @param
     * @return null
    */
    @GetMapping("selectAll")
    public PageInfo<IProjects> selectAll(int startpage, int pagesize){
        PageHelper.startPage(startpage,pagesize);
        Page<IProjects> page = (Page<IProjects>) this.iProjectsService.selectAll();
        PageInfo<IProjects> iProjectsPageInfo = new PageInfo<>(page);
        return iProjectsPageInfo;
    }

    /**
     * Description : 分页查询  <br/>
     * ChangeLog : 1. 创建 ( 21:48 );
     * @param iprojectsDto
     * @return com.github.pagehelper.PageInfo<com.woniuxy.entity.IProjects>
    */
    @PostMapping("search")
    public PageInfo<IProjects> search(@RequestBody IprojectsDto iprojectsDto){
        PageHelper.startPage(iprojectsDto.getPage(),iprojectsDto.getLimit());
        Page<IProjects> page = (Page<IProjects>) this.iProjectsService.queryAll(iprojectsDto.getIprojectsDtos());
        PageInfo<IProjects> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }


    /**
     * Description : 新增  <br/>
     * ChangeLog : 1. 创建 ( 21:49 );
     * @param  iProjects
     * @return String
    */
    @PostMapping("insert")
    public String insert(@RequestBody IProjects iProjects){
        int insert = this.iProjectsService.insert(iProjects);
        if (insert > 0){
            return "success";
        }
        return "failure";
    }

    /**
     * Description : 更改  <br/>
     * ChangeLog : 1. 创建 ( 21:57 );
     * @param iProjects
     * @return String
    */
    @PostMapping("update")
    public String update(@RequestBody IProjects iProjects){
        int i = this.iProjectsService.update(iProjects);
        if (i > 0){
            return "success";
        }
        return "failure";
    }

    @PostMapping("delete/{iprjId}")
    public String delete(@PathVariable("iprjId") int iprjId){
        boolean b = this.iProjectsService.deleteById(iprjId);
        if (b){
            return "success";
        }
        return "failure";
    }

    @GetMapping("selectAlls")
    public List<IProjects> selectAlls(){
        List<IProjects> list=this.iProjectsService.selectAll();
        return list;
    }

    /**
     * Description : 返回前端项目数据  <br/>
     * ChangeLog : 1. 创建 (2021/5/24 16:22 );
     * @param iprojectsDto
     * @return iproject对象
    */
    @PostMapping("selectiProject")
    @ApiOperation("项目数据")
    public ResponseEntity<PageInfo<IProjects>> selectiProject(@RequestBody IprojectsDto iprojectsDto){
        System.out.println("获取项目数据"+iprojectsDto);
        PageHelper.startPage(iprojectsDto.getPage(),iprojectsDto.getLimit());
        Page<IProjects> page1 = (Page<IProjects>) this.iProjectsService.queryByFid(iprojectsDto.getIprojectsDtos().getIprjPname());
        PageInfo<IProjects> pageInfo = new PageInfo<>(page1);
        if (pageInfo!=null){
            return new ResponseEntity(1,"查询项目数据成功",pageInfo);
        }
        return new ResponseEntity(0,"查询项目数据失败");
    }

}
